package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.TransferDto;
import com.crm.entity.*;
import com.crm.mapper.CluesMapper;
import com.crm.mapper.TransferMapper;
import com.crm.mapper.UserMapper;
import com.crm.result.PageResult;
import com.crm.service.TransferService;
import com.crm.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TransferServiceImpl extends ServiceImpl<TransferMapper, User> implements TransferService {

    private final TransferMapper transferMapper;
    private final CluesMapper cluesMapper;
    private final UserMapper userMapper;

    @Override
    public PageResult getTransferPageWithConditions(TransferDto transferDto) {
        Page<User> page = Page.of(transferDto.getPageNum(), transferDto.getPageSize());

        //分页条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.like(StringUtils.hasText(transferDto.getPhoneNumber()),User::getPhoneNumber,transferDto.getPhoneNumber());
        wrapper.eq(StringUtils.hasText(transferDto.getUserName()),User::getUserName,transferDto.getUserName());
        wrapper.between(StringUtils.hasText(transferDto.getCreateTime()), User::getCreateTime,transferDto.getParams().get("beginCreateTime"),transferDto.getParams().get("endCreateTime"));

        Page<User> contractPage = transferMapper.selectPage(page, wrapper);

        List<User> users = contractPage.getRecords();
        for (User user : users) {
            //商机数
            Integer businessCount = cluesMapper.getBusinessCount(user.getUserId());
            user.setBusinessNum(businessCount);
            //线索数
            Integer clueCount = cluesMapper.getClueCount(user.getUserId());
            user.setClueNum(clueCount);
            //更新
            userMapper.updateById(user);
        }


        return new PageResult(contractPage.getTotal(),contractPage.getRecords());
    }

    @Override
    public void setTransfer(Integer typeId, Integer oldId, Integer userId) {
        //根据typeid看转派的是线索还是商机
            List<Clue> clueList = cluesMapper.selectList(Wrappers.<Clue>lambdaQuery().eq(Clue::getTransfer, typeId).eq(Clue::getUserid, oldId));
            for (Clue clue : clueList) {
                //获取转派人的信息
                User transferUser = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUserId, userId));
                //设置转派
                clue.setUserid(userId);
                clue.setAssignby(userId);
                clue.setAssignName(transferUser.getUserName());
                //保存修改
                cluesMapper.update(clue,Wrappers.<Clue>lambdaQuery().eq(Clue::getUserid, oldId));
                //删除用户信息
                userMapper.delete(Wrappers.<User>lambdaQuery().eq(User::getUserId, oldId));
            }
    }
}
